﻿/*
 *  File: CGetHotelURLs.cs
 *  --------------------- 
 *  This class allows to scrape hotel information including hotel id, name, description,
 *  activities, services, internet, packing, checkin, checkout from a certain url.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataCrawler
{
    // Record that holds hotel information.
    public class HotelInfoStruct
    {
        public int id;
        public string name;
        public string general;
        public string activities;
        public string services;
        public string internet;
        public string parking;
        public string checkin;
        public string checkout;
    }

    public class CGetHotelInformation: CDataCrawler
    {
        // ----- Protected methods -----

        protected HotelInfoStruct hotelInfo;

        protected override void Handle()
        {
            hotelInfo = new HotelInfoStruct();
            
            // Get general text.
            var parentNode = document.DocumentNode.SelectSingleNode(".//*[@id='hc_htl_amenities']/div[1]/div[2]");

            if (parentNode == null) return;

            var node = document.DocumentNode.SelectSingleNode(".//*[@id='hc_htl_amenities']/div[1]/div[2]/p[1]");
            if (node != null)
                hotelInfo.general = node.InnerText;

            // Get activities text.
            node = document.DocumentNode.SelectSingleNode(".//*[@id='hc_htl_amenities']/div[1]/div[2]/p[2]"); 
            if (node != null)
                hotelInfo.activities = node.InnerText;

            // Get services text.
            node = parentNode.SelectSingleNode("//p[3]");
            if (node != null)
                hotelInfo.services = node.InnerText;

            // Get internet text.
            node = parentNode.SelectSingleNode("//p[4]");
            if (node != null)
                hotelInfo.internet = node.InnerText;

            // Get parking text.
            node = parentNode.SelectSingleNode("//p[5]");
            if (node != null)
                hotelInfo.parking = node.InnerText;

            // Get check-in text.
            node = parentNode.SelectSingleNode("//p[6]");
            if (node != null)
                hotelInfo.checkin = node.InnerText;

            // Get check-out text.
            node = parentNode.SelectSingleNode("//p[7]");
            if (node != null)
                hotelInfo.checkout = node.InnerText;

            // Get hotel name.
            node = document.DocumentNode.SelectSingleNode(".//*[@class='hc_htl_intro_name']");
            if (node != null)
                hotelInfo.name = node.Attributes["content"].Value;

            // Get hotel id.
            node = document.DocumentNode.SelectSingleNode(".//*[@class='hc_f_btn_v9 hc_f_submit']");
            if (node != null)
            {
                try
                {
                    string str = node.Attributes["onclick"].Value;
                    int startIndex = str.IndexOf("hotelID:'") + 9;
                    int endIndex = str.IndexOf("'});");
                    hotelInfo.id = Convert.ToInt32(str.Substring(startIndex, endIndex - startIndex));
                }
                catch (Exception ex)
                {
                    hotelInfo.id = -1;
                }
            }
        }

        // ----- Public methods -----

        public CGetHotelInformation(string url)
            : base(url)
        {            
        }

        // For debugging purpose only.
        public override string ToString()
        {
            string result = "\n ----- Hotel info record -----\n";
            result += "  ID: " + hotelInfo.id + "\n";
            result += "  Name: " + hotelInfo.name + "\n";
            result += "  General: " + hotelInfo.general + "\n";
            result += "  Activities: " + hotelInfo.activities + "\n";
            result += "  Services: " + hotelInfo.services + "\n";
            result += "  Internet: " + hotelInfo.internet + "\n";
            result += "  Parking: " + hotelInfo.parking + "\n";
            result += "  Check-in: " + hotelInfo.checkin + "\n";
            result += "  Check-out: " + hotelInfo.checkout + "\n";
            return result;
        }

        // Properties

        // Return HotelInfoStruct instance.
        public HotelInfoStruct HotelInfo
        {
            get { return hotelInfo; }
        }
    }
}
